package com.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.task.common.entity.sys.Role;
import com.task.common.entity.sys.UserRole;
import com.task.common.util.model.FieldUtil;
import com.task.common.util.string.StringUtils;
import com.task.mapper.RoleMapper;
import com.task.mapper.UserRoleMapper;
import com.task.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Huzz
 * @since 2021-10-11
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Role> getLoginUserRoleByUserId(Integer userId) {
        return roleMapper.getLoginUserRoleByUserId(userId);
    }

    @Override
    public boolean addRole(Role role) {
        FieldUtil.setCreatedAndUpdateField(role);
        return this.baseMapper.insert(role) > 0;
    }

    @Override
    public boolean updateRoleById(Role role) {
        FieldUtil.setUpdateField(role);
        return this.baseMapper.updateById(role) > 0;
    }

    @Override
    public boolean roleDistribute(Integer roleId, Integer userId) {
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(userId);
        FieldUtil.setCreatedAndUpdateField(userRole);
        userRoleMapper.insert(userRole);
        return userRoleMapper.insert(userRole) > 0;
    }

    @Override
    public Role finUserByRoleName(String roleName) {

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getName, roleName);

        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public Role finRoleById(Integer roleId) {
        return this.baseMapper.selectById(roleId);
    }

    @Override
    public Page<Role> finRolePage(Integer pageNum, Integer pageSize, Role model) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotEmpty(model.getName())) {
            wrapper.eq(Role::getName, model.getName());
        }
        if (StringUtils.isNotEmpty(model.getDescription())) {
            wrapper.eq(Role::getDescription, model.getDescription());
        }
        // if (model.getId() != null) {
        //     wrapper.eq(Role::getId, model.getId());
        // }
        // if (model.getStatus() != null) {
        //     wrapper.eq(Role::getStatus, model.getStatus());
        // }
        // TODO 优化
        wrapper.ne(Role::getName, "");

        Page<Role> page = new Page(pageNum, pageSize);

        return this.baseMapper.selectPage(page, wrapper);
    }

    @Override
    public boolean delRoleById(Integer roleId) {
        // 若role存在与user之间的联系, 不能删除
        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.eq(UserRole::getRoleId, roleId);
        if (StringUtils.isNotNull(userRoleMapper.selectList(userRoleLambdaQueryWrapper))) {
            return false;
        }
        return this.baseMapper.deleteById(roleId) > 0;
    }
}
